package com.xmb.wechat.view.alipay;

import android.content.Intent;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.AliPayBillBean;
import com.xmb.wechat.definterface.CommonIAndSListener;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.delegate.AlipayBillDelegate;
import com.xmb.wechat.dialog.HintDialog;
import com.xmb.wechat.helper.DataHelper;
import com.xmb.wechat.widget.TitleLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/21 09:27
 */
public class AliPayBillActivity extends BaseActivity {

    private static final int REQUEST_CODE_ADD = 102;

    @BindView(R2.id.title_layout)
    TitleLayout mTitleLayout;

    private AlipayBillDelegate mDelegate;
    private HintDialog         mHintDialog;

    public AliPayBillActivity() {
        super(R.layout.activity_alipay_bill);
    }

    @Override
    protected void initView() {
        mDelegate = new AlipayBillDelegate(this);
        mTitleLayout.setRightClick(v -> {
            if (mDelegate.getData().size() > 0) {
                if (mHintDialog == null) {
                    mHintDialog = new HintDialog(AliPayBillActivity.this, "确定清空数据?", new HintListener() {
                        @Override
                        public void onConfirm() {
                            mDelegate.clearData();
                            WechatStartApplication.getBoxStore().boxFor(AliPayBillBean.class).removeAll();
                        }

                        @Override
                        public void onCancel() {
                            //do nothing
                        }
                    });
                }
                mHintDialog.show();
            } else {
                ToastUtils.showShort("暂无数据");
            }
        });
        refresh();
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                AliPayBillActivity.this.finish();
            }
        });
    }

    private void refresh() {
        mDelegate.clearData();
        mDelegate.addData(WechatStartApplication.getBoxStore().boxFor(AliPayBillBean.class).query().build().find());
    }

    @OnClick({R2.id.btn_add, R2.id.btn_preview})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btn_add) {
            showListDialog(DataHelper.getBillTypeData(), new CommonIAndSListener() {
                @Override
                public void onChoose(int i, String s) {
                    AliPayBillAddActivity.start(AliPayBillActivity.this, i, REQUEST_CODE_ADD);
                }
            });
        } else if (id == R.id.btn_preview) {
            AliPayBillDetailActivity.start(AliPayBillActivity.this);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD)
            refresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHintDialog != null) {
            mHintDialog.dismiss();
            mHintDialog = null;
        }
    }
}
